<?php

namespace App\Models;

class NoticeModel
{
    public static function getNewNotice($conn){
        $noticeSql = "SELECT `id`, `title`,`attached`, `content`,`time` FROM `notice` WHERE `status`=1 and type = 1 order by time desc limit 1";
        return $conn->readConn($noticeSql);
    }

    public static function getNoticeList($conn,$NoticeExample){
        $noticeSql = "SELECT `id`, `title`,`attached`,`content`,`time` FROM `notice` WHERE `status`=1 and `type` = 1 order by time desc limit $NoticeExample->pageStart,$NoticeExample->limit";
        return $conn->readConn($noticeSql, true);
    }

    public static function getNoticeDetails($conn,$NoticeExample){
        $noticeSql = "SELECT `id`, `title`,`attached`, `content`,`time` FROM `notice` WHERE `id` = $NoticeExample->id and `status`=1 order by time desc limit 1";
        return $conn->readConn($noticeSql);
    }
    /**
     * 添加公告
     * entry
     * return int
     * */
    public static function insertNotice($conn,$NoticeExample){
        $fundsSql ="INSERT INTO `notice`(`title`, `attached`, `content`, `status`, `type`) VALUES ('$NoticeExample->title','$NoticeExample->attached','$NoticeExample->content','$NoticeExample->status','$NoticeExample->type')";
        return $conn->executeConn($fundsSql);
    }
    /**
     * 修改公告可添加字段
     * entry BannerExample
     * return int
     * */
    public static function upNoticeInside($conn,$NoticeExample){
        $joinSql="";
        if($NoticeExample->title !=null){
            $joinSql.=" `title` = '$NoticeExample->title',";
        }
        if($NoticeExample->attached !=null){
            $joinSql.=" `attached` = '$NoticeExample->attached',";
        }
        if($NoticeExample->content !=null){
            $joinSql.=" `content` = '$NoticeExample->content',";
        }
        if($NoticeExample->status !=null){
            $joinSql.=" `status` = '$NoticeExample->status',";
        }
        if($NoticeExample->type !=null){
            $joinSql.=" `type` = '$NoticeExample->type',";
        }
        if($joinSql !=null){
            $innerJoin = rtrim($joinSql,",");
            $upUserSql="UPDATE  `notice` SET $innerJoin WHERE `id` = '$NoticeExample->id'";
            return $conn->executeConn($upUserSql);
        }
    }
}